package cn.bonoon.kernel.expression.results;

import java.util.Date;

import org.batatasframework.web.RequestParameterReader;

import cn.bonoon.kernel.expression.ExpressionParseContext;
import cn.bonoon.kernel.expression.ExpressionValue;
import cn.bonoon.kernel.support.IOperator;

public class UserExpressionValue implements ExpressionValue{
	private final String field, args;
	
	public UserExpressionValue(ExpressionParseContext context, String field, String args){
		this.field = field;
		this.args = args;
	}

	//id/owner[oi]/username[un]/displayname[dn]/isSuper[is]/isAdmin[ia]/now/get
	@Override
	public Object value(IOperator operator, RequestParameterReader parameterReader, Object defaultValue) {
		String fn = field.toLowerCase();
		switch (fn) {
		case "id":
			return operator.getId();
		case "ownerid":
		case "owner":
		case "oi":
			return operator.getOwnerId();
		case "username":
		case "un":
			return operator.getUsername();
		case "displayname":
		case "dn":
			return operator.getDisplayName();
		case "issuper":
		case "is":
			return operator.isSuper();
		case "isadmin":
		case "ia":
			return operator.isAdmin();
		case "now":
			return new Date();
		case "get":
			return operator.get(args);
		}
		return defaultValue;
	}
}
